package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.BusinessTool;
import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.StrongUuidGenerator;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 身份验证表
 */
//@BusinessDataConverter(
//        insertSql = "insert into nd_authentication_data (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, IDENTIFIER, ID_CARD, VERIFY_FAIL, VERIFY_MEG, WHITE_LIST, IDENTITY_TYPE, CUSTOMER_TYPE, LEGAL_PERSON_NAME, SOCIAL_CREDIT_CODE, LEGAL_REPRESENTATIVE)" +
//                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
//        updateSql = "UPDATE nd_authentication_data SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,IDENTIFIER = ?,ID_CARD = ?,VERIFY_FAIL = ?,VERIFY_MEG = ?,WHITE_LIST = ?,IDENTITY_TYPE = ?,CUSTOMER_TYPE = ?,LEGAL_PERSON_NAME = ?,SOCIAL_CREDIT_CODE = ?,LEGAL_REPRESENTATIVE = ? WHERE ID = ?",
//        findMigratedIdListSql = "SELECT ID FROM nd_authentication_data WHERE CREATE_NAME = ?"
//)
public class NdAuthenticationData extends AbstractBusiness{
    private static StrongUuidGenerator uuidGenerator = new StrongUuidGenerator();

    public NdAuthenticationData(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }



    @Override
    public void migrate() throws ScriptException, Exception {
        List<Map> BtIemeIndentifyResultList = this.sourceData.getBtIemeIndentifyResult();
        for(Map btIemeIndentifyResult:BtIemeIndentifyResultList){
            try {
                String sourceId = ((Timestamp)btIemeIndentifyResult.get("CREATE_TIME")).getTime()+"-"+this.projectId;
                int needMigratedResult = this.needMigrated(sourceId);
                if (needMigratedResult == 0) {
                    continue;
                };
                // 单条插入或更新
                if (needMigratedResult == 1) {
//                    this.targetDbUtil.updateByParams(updateSql, this.buildValues(btIemeIndentifyResult, sourceId), true);
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchUpdateValues.add(this.buildValues(row));
                } else {
//                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btIemeIndentifyResult, sourceId));
                    // 批量插入或更新 和 单条插入或更新 二选一
//             this.batchInsertValues.add(this.buildValues(row));
                }

            } catch (Exception e) {
                throw new Exception(String.format("身份验证表[%s]迁移失败:%s\n", btIemeIndentifyResult.get("ITEMNO"), e.getMessage()));
            }


        }

    }
    public List buildValues (Map btIemeIndentifyResult, String id ) throws SQLException, ScriptException {
        List values = new ArrayList();
        //ID
        values.add(id);
        //CREATE_NAME	创建人名称
        values.add(this.projectId);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btIemeIndentifyResult.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btIemeIndentifyResult.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(1);
        //IDENTIFIER	自然人
        String Identifier = "";
        if(btIemeIndentifyResult.get("RESULTDESC").toString().substring(0,4).equals("个人姓名")){
            String ReSultDesc = btIemeIndentifyResult.get("RESULTDESC").toString();
            Identifier = ReSultDesc.substring(ReSultDesc.indexOf('[')+1,ReSultDesc.indexOf(']'));
        }
        values.add(Identifier);
        //ID_CARD	自然人身份证号码
        String IdCard = null;
        if(btIemeIndentifyResult.get("RESULTDESC").toString().substring(0,4).equals("个人姓名")){
            String ReSultDesc = btIemeIndentifyResult.get("RESULTDESC").toString();
            IdCard = ReSultDesc.substring(ReSultDesc.lastIndexOf('[')+1,ReSultDesc.lastIndexOf(']'));
        }
        values.add(IdCard);
        //TODO VERIFY_FAIL	验证是否通过  0一致 1不匹配
        int VeerifyFail = 0;
        if(btIemeIndentifyResult.get("SFLAG").toString().equals("0")){
            VeerifyFail = 1;
        }
        values.add(VeerifyFail);
        //VERIFY_MEG	验证返回消息
        values.add(btIemeIndentifyResult.get("RESULTDESC"));
        //WHITE_LIST	白名单
        values.add(1);
        //IDENTITY_TYPE	证件类型
        values.add(null);
        //CUSTOMER_TYPE	客户类型
        String CustomerType = "法人";
        if(btIemeIndentifyResult.get("RESULTDESC").toString().substring(0,4).equals("个人姓名")){
            CustomerType = "自然人";
        }
        values.add(CustomerType);
        //LEGAL_PERSON_NAME	法人名称
        String LegalPersonName = null;
        if(btIemeIndentifyResult.get("RESULTDESC").toString().substring(0,4).equals("企业名称")){
            String ReSultDesc = btIemeIndentifyResult.get("RESULTDESC").toString();
            LegalPersonName = ReSultDesc.substring(ReSultDesc.indexOf("["),ReSultDesc.indexOf("]"));
        }
        values.add(LegalPersonName);
        //SOCIAL_CREDIT_CODE	统一社会信用代码
        String SocialCreditCode = null;
        if(btIemeIndentifyResult.get("RESULTDESC").toString().substring(0,4).equals("企业名称")){
            String ReSultDesc = btIemeIndentifyResult.get("RESULTDESC").toString();
            SocialCreditCode = ReSultDesc.substring(ReSultDesc.indexOf("[")+1,ReSultDesc.indexOf("]")+1);
        }
        values.add(SocialCreditCode);
        //LEGAL_REPRESENTATIVE	法定代表人姓名
        String LegalRepresenTative = null;
        if(btIemeIndentifyResult.get("RESULTDESC").toString().substring(0,4).equals("企业名称")){
            String ReSultDesc = btIemeIndentifyResult.get("RESULTDESC").toString();
            LegalRepresenTative = ReSultDesc.substring(ReSultDesc.lastIndexOf("["),ReSultDesc.lastIndexOf("]"));
        }
        values.add(LegalRepresenTative);
        return values;
    }
}



